package dbzx.pdf;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;

import org.junit.Test;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

public class CreateSimplePdf {
	public static String FONT = "C:/WINDOWS/Fonts/simsun.ttc,0";
	
	/** 封面一级标题 */
	public static Font firstCoverFont = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED,10,Font.NORMAL,BaseColor.BLACK);
	
	
	@Test
	public void createSimplePdf() throws DocumentException, IOException {
		String path = "E:/demo/pdfCreat/"+System.currentTimeMillis()+".pdf";//输出pdf的路径
		Document doc = new Document();
		PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(path));
		doc.open();
		String text = readfile("src/main/resources/file/pdf/test.text");
		Paragraph paragraph = new Paragraph(text,firstCoverFont);
		/** 设置行间距，俩个参数：float fixedLeading, float multipliedLeading，
		 * 总的行间距可以用getTotalLeading()来查看， 可以理解是两行文字，顶部到顶部的距离，如果是0，两行文字是重叠的。
		 * 计算方式：fixedLeading+multipliedLeading*fontSize。在pdfDocument中是当前字体；在ColumnText，是指最大字体。
		 * 其中fixedLeading是固定参数，默认值是：1.5倍的fontsize
		 * multipliedLeading是可变参数，默认值是0.
		 */
		paragraph.setLeading(10,2);//设置行间距
		paragraph.setAlignment(Element.ALIGN_CENTER);//设置对齐方式：居中、左对齐、右对齐
		paragraph.setFirstLineIndent(20f);//设置首行缩进
		
		paragraph.setExtraParagraphSpace(200f);//设置段落之间的空白，测试无效，可以没找到正确的书写格式
		paragraph.setIndentationLeft(10f);//设置段落整体的左缩进
		paragraph.setIndentationRight(20f);//设置段落整体的右缩进
		paragraph.setPaddingTop(10f);//设置距离上一元素的顶部padding
		paragraph.setSpacingAfter(10f);//设置段落下方的空白距离
		paragraph.setSpacingBefore(10f);//设置段落上方的留白，,如果的本页的第一个段落，该设置无效
		doc.add(paragraph);
		doc.add(new Paragraph(text,firstCoverFont));
		Phrase phrase = new Phrase(text,firstCoverFont);
//		phrase.setLeading(10,4);
		
		System.out.println(phrase.getFont().getSize());
		System.out.println(phrase.getLeading());
		System.out.println(phrase.getTotalLeading());
		doc.add(phrase);
		Chunk chunk = new Chunk("测试chunk",firstCoverFont);
		chunk.setBackground(BaseColor.GREEN);//文字背景色
		chunk.setLineHeight(10);//行高
		chunk.setUnderline(2, 3);//下划线，或者文字任意文字的线条
		doc.add(chunk);
		
		//将文字放在页面上的任意地点
		Rectangle rect = new Rectangle(300, 300, 400, 400);//文本框位置
		rect.setBorder(Rectangle.LEFT);//显示边框，默认不显示，常量值：LEFT, RIGHT, TOP, BOTTOM，BOX,
		rect.setBorderWidth(1f);//边框线条粗细
		rect.setBorderColor(BaseColor.GREEN);//边框颜色
		PdfContentByte cb = writer.getDirectContent();
		cb.rectangle(rect);
		
		ColumnText ct = new ColumnText(cb);
		ct.addText(new Phrase("test",firstCoverFont));
		ct.setSimpleColumn(rect);
		ct.setUseAscender(false);
		ct.go();
		
		
		
		ColumnText cts = new ColumnText(writer.getDirectContent());
		cts.addText(new Phrase("test",firstCoverFont));
		cts.setSimpleColumn(rect);
		cts.go();
		
		doc.close();
		
	}
	
	public String readfile(String path) throws IOException {
		StringBuffer sb = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
		String line;
		while( (line =br.readLine()) != null) {
			sb.append(line);
		}
		br.close();
		return sb.toString();
	}
}
